﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Constants;
using SaleSoft.Models.ScreenClient;
using SaleSoft.Helper;
using Telerik.Web.Mvc;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class LoaiBannerController : BaseController {
        #region Properties Section

        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            LoaiBannerScreenClient screen = new LoaiBannerScreenClient();
            if (Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT] != null) {
                screen = (LoaiBannerScreenClient)Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index(){
            LoaiBannerScreenClient screen = new LoaiBannerScreenClient();
            if (Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT] != null) {
                screen = (LoaiBannerScreenClient)Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }
        #endregion

        #region CRUD Section
        [GridAction]
        [HttpPost]
        public ActionResult _Insert (SaleSoft.Models.BaseClient.LoaiBanner cLoai ) {
            if (ModelState.IsValid) {
                loai_banner loai = new loai_banner();
                ObjectHelper.Copy(cLoai, loai);
                db.loai_banner.AddObject(loai);
                db.SaveChanges();
            }
            LoaiBannerScreenClient screen = new LoaiBannerScreenClient();
            if (Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT] != null) {
                screen = (LoaiBannerScreenClient)Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _Update ( int id) {
            if (ModelState.IsValid) {
                SaleSoft.Models.BaseClient.LoaiBanner cLoai = new Models.BaseClient.LoaiBanner();
                TryUpdateModel(cLoai);
                cLoai.id_loaibanner = id;
                loai_banner loai = db.loai_banner.Single(i => i.id_loaibanner == id);
                ObjectHelper.Copy(cLoai, loai);
                db.ObjectStateManager.ChangeObjectState(loai, EntityState.Modified);
                db.SaveChanges();
            }
            LoaiBannerScreenClient screen = new LoaiBannerScreenClient();
            if (Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT] != null) {
                screen = (LoaiBannerScreenClient)Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _Delete ( int id) {
            loai_banner loai = db.loai_banner.Single(i => i.id_loaibanner == id);
            if (loai.ds_banner.Count == 0) {
                db.loai_banner.DeleteObject(loai);
                db.SaveChanges();
            } else { 
                
            }
            LoaiBannerScreenClient screen = new LoaiBannerScreenClient();
            if (Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT] != null) {
                screen = (LoaiBannerScreenClient)Session[ScreenClientConstants.LOAI_BANNER_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSLoai));
        }


        #endregion

        #region Helper Section
        private void Search ( LoaiBannerScreenClient screen ) {
            if (screen == null) {
                screen = new LoaiBannerScreenClient();
            }
            var loaibanners = db.loai_banner;
            screen.DSLoai.Clear();
            foreach (loai_banner loai in loaibanners) {
                SaleSoft.Models.BaseClient.LoaiBanner cLoai = new Models.BaseClient.LoaiBanner();
                ObjectHelper.Copy(loai, cLoai);
                cLoai.Deleteable = (loai.ds_banner.Count == 0) ? true : false;
                screen.DSLoai.Add(cLoai);
            }
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        #endregion
    }
}